﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Excel;
namespace BYManageInfoSystem
{
    public partial class Frm_MonthFeeShouldRecvNotRecv : Form
    {
        private int m_nSelectFeeTTLoanTabID = 0;
        private DGBY.BLL.FacFeeTab m_bllFacFeeTab = new DGBY.BLL.FacFeeTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.FeeTab m_bllFeeTab = new DGBY.BLL.FeeTab();
        private DGBY.BLL.CargoBoxTab m_bllCargoBoxTab = new DGBY.BLL.CargoBoxTab();
        private DGBY.BLL.FacFeeRelationTab m_bllFacFeeRelationTab = new DGBY.BLL.FacFeeRelationTab();
        private Dictionary<string, DataRow> dicFacName2DataRow = new Dictionary<string, DataRow>();
        private DGBY.BLL.FeeInvoiceTab m_bllFeeInvoiceTab = new DGBY.BLL.FeeInvoiceTab();

        private DGBY.BLL.FeeTrueRecvRegPrivateTab m_bllFeeTrueRecvRegPrivateTab = new DGBY.BLL.FeeTrueRecvRegPrivateTab();
        private DGBY.BLL.FeeTrueRecvRegPubByYSTab m_bllFeeTrueRecvRegPubByYSTab = new DGBY.BLL.FeeTrueRecvRegPubByYSTab();
        private DGBY.BLL.FeeTrueRecvRegPubTCTab m_bllFeeTrueRecvRegPubTCTab = new DGBY.BLL.FeeTrueRecvRegPubTCTab();

        private DGBY.BLL.FeeShouldRecvNotRecvTab m_bllFeeShouldRecvNotRecvTab = new DGBY.BLL.FeeShouldRecvNotRecvTab();


        private Dictionary<string, DataRow> m_dicFacName2DataRow2 = new Dictionary<string, DataRow>();
        public class CTotal
        {
            public Decimal dtShouldRecv = 0;
            public Decimal dtTransPub = 0;
            public Decimal dtPrivateTotal = 0;
            public Decimal dtPrivateZX = 0;
            public Decimal dtPrivateSJ = 0;
            public Decimal dtPrivateTC = 0;
            public Decimal dtTrueRecvTotal = 0;
            public Decimal dtDiff = 0;
        }
        public class CustomMergedCellEvaluator : Infragistics.Win.UltraWinGrid.IMergedCellEvaluator
        {
            // 合并日期相同的单元格（无需时间精确相同）
            public bool ShouldCellsBeMerged(UltraGridRow row1, UltraGridRow row2, UltraGridColumn column)
            {
                string str1 = (string)row1.GetCellValue(column);
                string str2 = (string)row2.GetCellValue(column);

                return str1 == str2;

            }

        }
        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        public Frm_MonthFeeShouldRecvNotRecv()
        {
            InitializeComponent();
        }
        private void MakeColumnTrue(DataTable dt)
        {
            dt.Columns.Add("序号", System.Type.GetType("System.Int32"));
            dt.Columns.Add("部门", System.Type.GetType("System.String"));
            dt.Columns.Add("客户", System.Type.GetType("System.String"));
            dt.Columns.Add("月份", System.Type.GetType("System.String"));
            dt.Columns.Add("应收未收费", System.Type.GetType("System.Decimal"));

            dt.Columns.Add("更改后应收未收费", System.Type.GetType("System.Decimal"));
        }
        private void ShowAllRecordInDataGridViewTrue()
        {
            ultraGrid1.DataSource = null;
            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            DataSet ds = m_bllFeeShouldRecvNotRecvTab.GetList(@"ShenBaoDate >= '" + dtFirst.Date + "'"
                                     + " and ShenBaoDate <= '" + dtEnd.Date + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Decimal dtTotal = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dtTotal += Convert.ToDecimal(ds.Tables[0].Rows[i]["更改后应收未收费"].ToString());
                }
                DataRow drTotal2 = ds.Tables[0].NewRow();
                drTotal2["部门"] = "合计";

                drTotal2["应收未收费"] = dtTotal;
                drTotal2["更改后应收未收费"] = dtTotal;
                ds.Tables[0].Rows.Add(drTotal2.ItemArray);
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[4].Hidden = true;
            }
            else
            {
                DataTable dtNew = new DataTable();
                MakeColumnTrue(dtNew);
                int nSeq = 0;
                Decimal dtTotal = 0;
                foreach (string strFacName in m_dicFacName2DataRow2.Keys)
                {
                    DataRow drTemp = m_dicFacName2DataRow2[strFacName];
                    nSeq++;
                    DataRow drTotal = dtNew.NewRow();
                    drTotal["序号"] = nSeq.ToString();
                    drTotal["部门"] = drTemp["所属部门"];
                    drTotal["客户"] = strFacName;
                    drTotal["月份"] = this.dateTimePickerDate.Value.Month.ToString() + "月";
                    if ((Decimal)drTemp["差额"] < 0)
                    {
                        drTotal["应收未收费"] = 0 - (Decimal)drTemp["差额"];
                        drTotal["更改后应收未收费"] = 0 - (Decimal)drTemp["差额"];
                    }
                    else
                    {
                        drTotal["应收未收费"] = 0 ;
                        drTotal["更改后应收未收费"] = 0;
                    }

                    dtTotal += (Decimal)drTotal["应收未收费"];
                    
                    dtNew.Rows.Add(drTotal.ItemArray);

                    DGBY.Model.FeeShouldRecvNotRecvTab modelFeeShouldRecvNotRecvTab = new DGBY.Model.FeeShouldRecvNotRecvTab();
                    modelFeeShouldRecvNotRecvTab.DepName = drTemp["所属部门"].ToString();
                    modelFeeShouldRecvNotRecvTab.FacName = strFacName;
                    modelFeeShouldRecvNotRecvTab.ShenBaoDate = this.dateTimePickerDate.Value;
                    modelFeeShouldRecvNotRecvTab.ShouldRecvNotRecvMoney = (Decimal)drTotal["应收未收费"];
                    modelFeeShouldRecvNotRecvTab.TrueShouldRecvNotRecvMoney = (Decimal)drTotal["更改后应收未收费"];

                    m_bllFeeShouldRecvNotRecvTab.Add(modelFeeShouldRecvNotRecvTab);

                }
                DataRow drTotal2 = dtNew.NewRow();
                drTotal2["部门"] = "合计";

                drTotal2["应收未收费"] = dtTotal;
                drTotal2["更改后应收未收费"] = dtTotal;
                dtNew.Rows.Add(drTotal2.ItemArray);
                ultraGrid1.DataSource = dtNew;
            }
        }

        private void Frm_MonthFeeShouldRecvNotRecv_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
        }

         /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }

        private void MakeColumn(DataTable dt)
        {
            dt.Columns.Add("工厂简称", System.Type.GetType("System.String"));
            dt.Columns.Add("订单号", System.Type.GetType("System.String"));
            dt.Columns.Add("合同号", System.Type.GetType("System.String"));
            dt.Columns.Add("柜号", System.Type.GetType("System.String"));
            dt.Columns.Add("柜数", System.Type.GetType("System.Int32"));

            dt.Columns.Add("百业收据", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("百业装卸发票", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("运输发票", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应收费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付费用", System.Type.GetType("System.Decimal"));

            dt.Columns.Add("商检金额", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付商检费", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应收商检费", System.Type.GetType("System.Decimal"));

            DataSet ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "商检" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收商检费合计", System.Type.GetType("System.Decimal"));


            dt.Columns.Add("运输公司", System.Type.GetType("System.String"));
            dt.Columns.Add("应付运输费", System.Type.GetType("System.Decimal"));
            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "运输" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }
            dt.Columns.Add("应收运输费合计", System.Type.GetType("System.Decimal"));


            dt.Columns.Add("报关行", System.Type.GetType("System.String"));
            dt.Columns.Add("报关单号", System.Type.GetType("System.String"));
            dt.Columns.Add("核销单号", System.Type.GetType("System.String"));
            dt.Columns.Add("报关金额", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("币种", System.Type.GetType("System.String"));
            dt.Columns.Add("经营单位", System.Type.GetType("System.String"));
            dt.Columns.Add("申报日期", System.Type.GetType("System.DateTime"));
            dt.Columns.Add("应付报关费", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }
            dt.Columns.Add("应收报关费合计", System.Type.GetType("System.Decimal"));

            dt.Columns.Add("公帐汇款船务费", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("私帐汇款船务费", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收单证费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付单证费用合计", System.Type.GetType("System.Decimal"));

            ds = m_bllFeeTab.GetFeeNameList("FeeCateTab.FeeCateName like " + "'%" + "其他" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strTemp = ds.Tables[0].Rows[i]["FeeName"].ToString();
                    dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                }
            }

            dt.Columns.Add("应收其他费用合计", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("应付其他费用合计", System.Type.GetType("System.Decimal"));
        }
        private void FillDataTable(DataTable dtNew, DataSet ds)
        {
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = dtNew.NewRow();
                    int nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["BGDComeBackTabID"].ToString(), 10);
                    DataSet ds2 = m_bllCargoBoxTab.GetAllCargoBoxInfo("CargoBoxTab.BGDComeBackTabID = " + nBGDComeBackTabID.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        dr["柜数"] = ds2.Tables[0].Rows.Count;
                        StringBuilder strSql = new StringBuilder();
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            strSql.Append(ds2.Tables[0].Rows[j]["集装箱号"].ToString());
                            strSql.Append("\n");
                        }
                        dr["柜号"] = strSql.ToString();
                    }
                    else
                    {
                        dr["柜数"] = 0;
                        dr["柜号"] = "";
                    }


                    dr["合同号"] = ds.Tables[0].Rows[i]["合同编号"];
                    dr["商检金额"] = ds.Tables[0].Rows[i]["商检金额"];
                    dr["应付商检费"] = ds.Tables[0].Rows[i]["应付商检金额"];
                    dr["应收商检费"] = ds.Tables[0].Rows[i]["应收商检金额"];

                    dr["经营单位"] = ds.Tables[0].Rows[i]["经营单位"];
                    dr["订单号"] = ds.Tables[0].Rows[i]["订单号"];
                    dr["工厂简称"] = ds.Tables[0].Rows[i]["工厂名称"];
                    dr["报关行"] = ds.Tables[0].Rows[i]["报关行名称"];
                    dr["报关单号"] = ds.Tables[0].Rows[i]["报关单号"];
                    dr["核销单号"] = ds.Tables[0].Rows[i]["核销单号"];
                    dr["币种"] = ds.Tables[0].Rows[i]["币种"];
                    dr["申报日期"] = ds.Tables[0].Rows[i]["申报日期"];

                    dr["报关金额"] = ds.Tables[0].Rows[i]["报关金额"];
                    dr["运输公司"] = ds.Tables[0].Rows[i]["运输公司"];
                    dr["经营单位"] = ds.Tables[0].Rows[i]["单证公司"];
                    dr["公帐汇款船务费"] = ds.Tables[0].Rows[i]["公帐汇款船务费"];
                    dr["私帐汇款船务费"] = ds.Tables[0].Rows[i]["私帐汇款船务费"];
                    dr["运输公司"] = ds.Tables[0].Rows[i]["运输公司"];
                    dr["应付运输费"] = ds.Tables[0].Rows[i]["应付运输费"];

                    dr["应付报关费"] = ds.Tables[0].Rows[i]["应付报关费"];
                    dr["应付单证费用合计"] = ds.Tables[0].Rows[i]["应付单证费"];
                    dr["应付其他费用合计"] = ds.Tables[0].Rows[i]["应付其他费"];

                    int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);
                    DataSet ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "商检" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收商检费合计"] = (Decimal)dr["应收商检费"] + dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "运输" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收运输费合计"] = dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收报关费合计"] = dtTotalTemp;
                    }
                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收单证费用合计"] = dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "其他" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                            dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());

                        }

                        dr["应收其他费用合计"] = dtTotalTemp;
                    }

                    dr["百业收据"] = (Decimal)dr["应收单证费用合计"] + (Decimal)dr["应收其他费用合计"] + (Decimal)dr["私帐汇款船务费"];
                    dr["百业装卸发票"] = (Decimal)dr["应收商检费合计"] + (Decimal)dr["应收报关费合计"] + (Decimal)dr["公帐汇款船务费"];
                    dr["运输发票"] = dr["应收运输费合计"];
                    dr["应收费用合计"] = (Decimal)dr["百业收据"] + (Decimal)dr["百业装卸发票"] + (Decimal)dr["运输发票"];
                    dr["应付费用"] = (Decimal)dr["应付商检费"] + (Decimal)dr["应付运输费"] + (Decimal)dr["应付报关费"]
                        + (Decimal)dr["应付单证费用合计"] + (Decimal)dr["应付其他费用合计"]
                        + (Decimal)dr["私帐汇款船务费"] + (Decimal)dr["公帐汇款船务费"];

                    //dtNew.Rows.Add(dr.ItemArray);
                    dicFacName2DataRow.Add(ds.Tables[0].Rows[i]["工厂名称"].ToString(), dr);
                }
            }
        }

        private void ShowAllRecordInDataGridView()
        {
            m_dicFacName2DataRow2.Clear();
            dicFacName2DataRow.Clear();
            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecEx(@"BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                                     + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dtNew2 = new DataTable();
                MakeColumn(dtNew2);
                FillDataTable(dtNew2, ds);

                ds = m_bllFacFeeTab.ShowFeeMonthRecvGeneralRec("BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                    + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    Dictionary<string, Decimal> m_dicTCName2Decimal = new Dictionary<string, Decimal>();
                    CTotal m_cTotal = new CTotal();


                    ds.Tables[0].Columns.Add("编号", System.Type.GetType("System.String"));
                    ds.Tables[0].Columns["编号"].SetOrdinal(4);
                    ds.Tables[0].Columns.Add("应收费用", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["应收费用"].SetOrdinal(6);

                    DataSet ds2 = m_bllFacFeeTab.ShowTCNameListRec("FeeInvoiceTab.TrueInvoiceJinE > 0"
                                        + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                                        + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'"
                                        + " and BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "运输" + "%'");
                    int nIndex = 7;
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int k = 0; k < ds2.Tables[0].Rows.Count; k++)
                        {

                            string strTcTemp = ds2.Tables[0].Rows[k]["拖车公司简称"].ToString();
                            ds.Tables[0].Columns.Add(strTcTemp, System.Type.GetType("System.Decimal"));
                            ds.Tables[0].Columns[strTcTemp].SetOrdinal(nIndex++);
                            m_dicTCName2Decimal.Add(strTcTemp, 0);
                        }
                    }


                    ds.Tables[0].Columns.Add("百业运输公帐收款", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["百业运输公帐收款"].SetOrdinal(nIndex++);
                    ds.Tables[0].Columns.Add("合计", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["合计"].SetOrdinal(nIndex++);

                    ds.Tables[0].Columns.Add("百业装卸发票", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["百业装卸发票"].SetOrdinal(nIndex++);
                    ds.Tables[0].Columns.Add("百业收据", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["百业收据"].SetOrdinal(nIndex++);
                    ds.Tables[0].Columns.Add("运输发票", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["运输发票"].SetOrdinal(nIndex++);
                    ds.Tables[0].Columns.Add("实收费用合计", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["实收费用合计"].SetOrdinal(nIndex++);
                    ds.Tables[0].Columns.Add("差额", System.Type.GetType("System.Decimal"));
                    ds.Tables[0].Columns["差额"].SetOrdinal(nIndex++);

                    DataTable dtNew = null;
                    dtNew = ds.Tables[0].Clone();

                    string strDepName = "";
                    DataRow drTotal2 = null;
                    Dictionary<string, Decimal> dicTCName2Decimal = null;
                    CTotal cTotal = null;
                    int nSeq = 0;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {

                        string strTemp = ds.Tables[0].Rows[i]["所属部门"].ToString();

                        string strDep = strTemp.Substring(0, 1);
                        DateTime dtSB = (DateTime)ds.Tables[0].Rows[i]["申报日期"];

                        string strYear = dtSB.Year.ToString();
                        string strMon = dtSB.Month.ToString();
                        string strFind = strDep + strYear + strMon;



                        if (strTemp.CompareTo(strDepName) != 0)//换部门了
                        {
                            nSeq = 0;
                            strDepName = strTemp;
                            if (drTotal2 != null)
                            {
                                drTotal2["应收费用"] = cTotal.dtShouldRecv;
                                DataSet ds5 = m_bllFacFeeTab.ShowTCNameListRec("FeeInvoiceTab.TrueInvoiceJinE > 0"
                        + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                        + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'"
                        + " and BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "运输" + "%'");

                                if (ds5.Tables.Count > 0 && ds5.Tables[0].Rows.Count > 0)
                                {
                                    for (int k = 0; k < ds5.Tables[0].Rows.Count; k++)
                                    {

                                        string strTcTemp = ds5.Tables[0].Rows[k]["拖车公司简称"].ToString();
                                        drTotal2[strTcTemp] = dicTCName2Decimal[strTcTemp];
                                    }
                                }

                                drTotal2["百业运输公帐收款"] = cTotal.dtTransPub;
                                drTotal2["合计"] = cTotal.dtPrivateTotal;
                                drTotal2["百业装卸发票"] = cTotal.dtPrivateZX;
                                drTotal2["百业收据"] = cTotal.dtPrivateSJ;
                                drTotal2["运输发票"] = cTotal.dtPrivateTC;
                                drTotal2["实收费用合计"] = cTotal.dtTrueRecvTotal;
                                drTotal2["差额"] = cTotal.dtDiff;

                                dtNew.Rows.Add(drTotal2.ItemArray);
                            }
                            nSeq++;
                            ds.Tables[0].Rows[i]["编号"] = strFind + nSeq.ToString().PadLeft(4, '0');

                            drTotal2 = dtNew.NewRow();
                            drTotal2["编号"] = "合计";
                            drTotal2["所属部门"] = strTemp;
                            dicTCName2Decimal = new Dictionary<string, decimal>();
                            DataSet ds7 = m_bllFacFeeTab.ShowTCNameListRec("FeeInvoiceTab.TrueInvoiceJinE > 0"
                    + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                    + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'"
                    + " and BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "运输" + "%'");

                            if (ds7.Tables.Count > 0 && ds7.Tables[0].Rows.Count > 0)
                            {
                                for (int k = 0; k < ds7.Tables[0].Rows.Count; k++)
                                {

                                    string strTcTemp = ds7.Tables[0].Rows[k]["拖车公司简称"].ToString();
                                    dicTCName2Decimal.Add(strTcTemp, 0);
                                }
                            }
                            cTotal = new CTotal();
                        }



                        string strFac = ds.Tables[0].Rows[i]["工厂简称"].ToString();
                        DataSet ds3 = m_bllFacFeeTab.ShowFeeInvoiceMonthRecvGeneralRec("BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                                                        + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'"
                                                        + " and FactoryDataTab.FactoryNameJX = '" + strFac + "'");
                        if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                        {
                            Decimal dtTotal1 = 0;
                            Decimal dtTotal2 = 0;
                            for (int j = 0; j < ds3.Tables[0].Rows.Count; j++)
                            {
                                string strKaiPiao = ds3.Tables[0].Rows[j]["开票种类名称"].ToString();
                                int nIsPrivate = Convert.ToInt32(ds3.Tables[0].Rows[j]["IsPrivate"].ToString(), 10);
                                if (strKaiPiao.IndexOf("收据") >= 0)
                                {
                                    string strFacName = ds.Tables[0].Rows[i]["工厂名称"].ToString();
                                    DataSet ds4 = m_bllFeeTrueRecvRegPrivateTab.GetAllNotLogisticsTruePayRegList("FactoryDataTab.FactoryName = '" + strFacName + "'"
                                                                                + " and ShenBaoDate >= '" + dtFirst.Date + "'"
                                                                                 + " and ShenBaoDate <= '" + dtEnd.Date + "'");
                                    if (ds4.Tables.Count > 0 && ds4.Tables[0].Rows.Count > 0)
                                    {
                                        ds.Tables[0].Rows[i]["百业收据"] = ds4.Tables[0].Rows[0]["百业收据实收费用"];
                                        dtTotal1 += (Decimal)ds4.Tables[0].Rows[0]["百业收据实收费用"];
                                        dtTotal2 += (Decimal)ds4.Tables[0].Rows[0]["百业收据实收费用"];
                                        m_cTotal.dtPrivateSJ += (Decimal)ds4.Tables[0].Rows[0]["百业收据实收费用"];
                                        cTotal.dtPrivateSJ += (Decimal)ds4.Tables[0].Rows[0]["百业收据实收费用"];
                                    }

                                }
                                else if (strKaiPiao.IndexOf("装卸") >= 0)
                                {
                                    string strFacName = ds.Tables[0].Rows[i]["工厂名称"].ToString();
                                    if (nIsPrivate != 0)
                                    {

                                        DataSet ds4 = m_bllFeeTrueRecvRegPrivateTab.GetAllNotLogisticsTruePayRegList("FactoryDataTab.FactoryName = '" + strFacName + "'"
                                                                                    + " and ShenBaoDate >= '" + dtFirst.Date + "'"
                                                                                     + " and ShenBaoDate <= '" + dtEnd.Date + "'");
                                        if (ds4.Tables.Count > 0 && ds4.Tables[0].Rows.Count > 0)
                                        {
                                            ds.Tables[0].Rows[i]["百业装卸发票"] = ds4.Tables[0].Rows[0]["百业装卸实收费用"];
                                            dtTotal1 += (Decimal)ds4.Tables[0].Rows[0]["百业装卸实收费用"];
                                            dtTotal2 += (Decimal)ds4.Tables[0].Rows[0]["百业装卸实收费用"];
                                            m_cTotal.dtPrivateZX += (Decimal)ds4.Tables[0].Rows[0]["百业装卸实收费用"];
                                            cTotal.dtPrivateZX += (Decimal)ds4.Tables[0].Rows[0]["百业装卸实收费用"];
                                        }
                                    }
                                    else
                                    {
                                        DataSet ds4 = m_bllFeeTrueRecvRegPubByYSTab.GetAllFeeTrueRecvRegPubByYSList("FactoryDataTab.FactoryName = '" + strFacName + "'"
                                                            + " and ShenBaoDate >= '" + dtFirst.Date + "'"
                                                             + " and ShenBaoDate <= '" + dtEnd.Date + "'");

                                        if (ds4.Tables.Count > 0 && ds4.Tables[0].Rows.Count > 0)
                                        {
                                            ds.Tables[0].Rows[i]["百业运输公帐收款"] = ds4.Tables[0].Rows[0]["实收费用"];
                                            dtTotal2 += (Decimal)ds4.Tables[0].Rows[0]["实收费用"];
                                            m_cTotal.dtTransPub += (Decimal)ds4.Tables[0].Rows[0]["实收费用"];
                                            cTotal.dtTransPub += (Decimal)ds4.Tables[0].Rows[0]["实收费用"];
                                        }
                                    }
                                }
                                else//if(strKaiPiao.IndexOf("运输") >= 0)
                                {
                                    string strFacName = ds.Tables[0].Rows[i]["工厂名称"].ToString();
                                    if (nIsPrivate != 0)
                                    {

                                        DataSet ds4 = m_bllFeeTrueRecvRegPrivateTab.GetAllNotLogisticsTruePayRegList("FactoryDataTab.FactoryName = '" + strFacName + "'"
                                                                                    + " and ShenBaoDate >= '" + dtFirst.Date + "'"
                                                                                     + " and ShenBaoDate <= '" + dtEnd.Date + "'");
                                        if (ds4.Tables.Count > 0 && ds4.Tables[0].Rows.Count > 0)
                                        {
                                            ds.Tables[0].Rows[i]["运输发票"] = ds4.Tables[0].Rows[0]["百业拖车实收费用"];
                                            dtTotal1 += (Decimal)ds4.Tables[0].Rows[0]["百业拖车实收费用"];
                                            dtTotal2 += (Decimal)ds4.Tables[0].Rows[0]["百业拖车实收费用"];
                                            m_cTotal.dtPrivateTC += (Decimal)ds4.Tables[0].Rows[0]["百业拖车实收费用"];
                                            cTotal.dtPrivateTC += (Decimal)ds4.Tables[0].Rows[0]["百业拖车实收费用"];
                                        }
                                    }
                                    else
                                    {
                                        DataSet ds4 = m_bllFeeTrueRecvRegPubTCTab.GetAllFeeTrueRecvRegPubTCList("FactoryDataTab.FactoryName = '" + strFacName + "'"
                                                            + " and ShenBaoDate >= '" + dtFirst.Date + "'"
                                                             + " and ShenBaoDate <= '" + dtEnd.Date + "'");

                                        if (ds4.Tables.Count > 0 && ds4.Tables[0].Rows.Count > 0)
                                        {
                                            string strTCName = ds4.Tables[0].Rows[0]["拖车公司简称"].ToString();
                                            ds.Tables[0].Rows[i][strTCName] = ds4.Tables[0].Rows[0]["实收费用"];
                                            dtTotal2 += (Decimal)ds4.Tables[0].Rows[0]["实收费用"];
                                            m_dicTCName2Decimal[strTCName] += (Decimal)ds4.Tables[0].Rows[0]["实收费用"];
                                            dicTCName2Decimal[strTCName] += (Decimal)ds4.Tables[0].Rows[0]["实收费用"];
                                        }
                                    }
                                }

                            }

                            ds.Tables[0].Rows[i]["合计"] = dtTotal1;
                            ds.Tables[0].Rows[i]["实收费用合计"] = dtTotal2;
                            string strFacName2 = ds.Tables[0].Rows[i]["工厂简称"].ToString();
                            ds.Tables[0].Rows[i]["应收费用"] = dicFacName2DataRow[strFacName2]["应收费用合计"];
                            ds.Tables[0].Rows[i]["差额"] = dtTotal2 - (Decimal)dicFacName2DataRow[strFacName2]["应收费用合计"];
                            
                            cTotal.dtShouldRecv += (Decimal)ds.Tables[0].Rows[i]["应收费用"];
                            cTotal.dtPrivateTotal += (Decimal)ds.Tables[0].Rows[i]["合计"];
                            cTotal.dtTrueRecvTotal += (Decimal)ds.Tables[0].Rows[i]["实收费用合计"];
                            cTotal.dtDiff += (Decimal)ds.Tables[0].Rows[i]["差额"];


                            m_cTotal.dtShouldRecv += (Decimal)ds.Tables[0].Rows[i]["应收费用"];
                            m_cTotal.dtPrivateTotal += (Decimal)ds.Tables[0].Rows[i]["合计"];
                            m_cTotal.dtTrueRecvTotal += (Decimal)ds.Tables[0].Rows[i]["实收费用合计"];
                            m_cTotal.dtDiff += (Decimal)ds.Tables[0].Rows[i]["差额"];
                        }


                        dtNew.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                        m_dicFacName2DataRow2.Add(strFac, ds.Tables[0].Rows[i]);
                    }
                    //最后一个部门的合计
                    drTotal2["应收费用"] = cTotal.dtShouldRecv;
                    DataSet ds8 = m_bllFacFeeTab.ShowTCNameListRec("FeeInvoiceTab.TrueInvoiceJinE > 0"
            + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
            + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'"
            + " and BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "运输" + "%'");

                    if (ds8.Tables.Count > 0 && ds8.Tables[0].Rows.Count > 0)
                    {
                        for (int k = 0; k < ds8.Tables[0].Rows.Count; k++)
                        {

                            string strTcTemp = ds8.Tables[0].Rows[k]["拖车公司简称"].ToString();
                            drTotal2[strTcTemp] = dicTCName2Decimal[strTcTemp];
                        }
                    }

                    drTotal2["百业运输公帐收款"] = cTotal.dtTransPub;
                    drTotal2["合计"] = cTotal.dtPrivateTotal;
                    drTotal2["百业装卸发票"] = cTotal.dtPrivateZX;
                    drTotal2["百业收据"] = cTotal.dtPrivateSJ;
                    drTotal2["运输发票"] = cTotal.dtPrivateTC;
                    drTotal2["实收费用合计"] = cTotal.dtTrueRecvTotal;
                    drTotal2["差额"] = cTotal.dtDiff;

                    dtNew.Rows.Add(drTotal2.ItemArray);


                    //总合计
                    DataRow drTotal = dtNew.NewRow();
                    drTotal["所属部门"] = "合计";
                    drTotal["应收费用"] = m_cTotal.dtShouldRecv;
                    DataSet ds6 = m_bllFacFeeTab.ShowTCNameListRec("FeeInvoiceTab.TrueInvoiceJinE > 0"
            + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
            + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'"
            + " and BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "运输" + "%'");

                    if (ds6.Tables.Count > 0 && ds6.Tables[0].Rows.Count > 0)
                    {
                        for (int k = 0; k < ds6.Tables[0].Rows.Count; k++)
                        {

                            string strTcTemp = ds6.Tables[0].Rows[k]["拖车公司简称"].ToString();
                            drTotal[strTcTemp] = m_dicTCName2Decimal[strTcTemp];
                        }
                    }

                    drTotal["百业运输公帐收款"] = m_cTotal.dtTransPub;
                    drTotal["合计"] = m_cTotal.dtPrivateTotal;
                    drTotal["百业装卸发票"] = m_cTotal.dtPrivateZX;
                    drTotal["百业收据"] = m_cTotal.dtPrivateSJ;
                    drTotal["运输发票"] = m_cTotal.dtPrivateTC;
                    drTotal["实收费用合计"] = m_cTotal.dtTrueRecvTotal;
                    drTotal["差额"] = m_cTotal.dtDiff;
                    dtNew.Rows.Add(drTotal.ItemArray);

                    //ultraGrid1.DataSource = dtNew;
                    //ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                    //ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
                    //ultraGrid1.DisplayLayout.Bands[0].Columns[2].Hidden = true;
                    //ProcessMultiHeader();
                }

            }


        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
            ShowAllRecordInDataGridViewTrue();
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            if (this.ultraGrid1.ActiveCell.Row.Index == this.ultraGrid1.Rows.Count - 1)
                return;
            string strFeeTTLoanTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectFeeTTLoanTabID = Convert.ToInt32(strFeeTTLoanTabID, 10);
            DGBY.Model.FeeShouldRecvNotRecvTab modelFeeShouldRecvNotRecvTab = m_bllFeeShouldRecvNotRecvTab.GetModel(m_nSelectFeeTTLoanTabID);
            this.textBoxMod.Text = modelFeeShouldRecvNotRecvTab.TrueShouldRecvNotRecvMoney.ToString();
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            DGBY.Model.FeeShouldRecvNotRecvTab modelFeeShouldRecvNotRecvTab = m_bllFeeShouldRecvNotRecvTab.GetModel(m_nSelectFeeTTLoanTabID);
            modelFeeShouldRecvNotRecvTab.TrueShouldRecvNotRecvMoney = Convert.ToDecimal(this.textBoxMod.Text);
            m_bllFeeShouldRecvNotRecvTab.Update(modelFeeShouldRecvNotRecvTab);
            ShowAllRecordInDataGridViewTrue();
            MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void ultraGrid1_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
        {
            ultraGrid1.DisplayLayout.Override.CellAppearance.TextHAlign = HAlign.Center;
            ultraGrid1.DisplayLayout.Override.CellAppearance.TextVAlign = VAlign.Middle;
            ultraGrid1.DisplayLayout.Override.MergedCellAppearance.TextHAlign = HAlign.Center;
            ultraGrid1.DisplayLayout.Override.MergedCellAppearance.TextVAlign = VAlign.Middle;

            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].MergedCellStyle = MergedCellStyle.Always;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].MergedCellEvaluator = new
   CustomMergedCellEvaluator();
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            
            // Get the cell being exporter
            UltraGridCell cell = e.GridRow.Cells[e.GridColumn];

            // Get the Merged cells. 
            UltraGridCell[] mergedCells = cell.GetMergedCells();


            // Check to see if there are any merged cells. If not, we don't need to do anything. 
            if (mergedCells != null)
            {
                // There are merged cells. Check to see if the cell being exported is the first one. 
                bool isFirstCell = true;
                foreach (UltraGridCell mergedCell in mergedCells)
                {
                    if (cell.Row.Index > mergedCell.Row.Index)
                    {
                        // If the cell being exported has a higher index that any of the cells
                        // it is merged with, them it's not the first cell and we should already
                        // have handled the merge. 
                        isFirstCell = false;
                        break;
                    }
                }

                // If it is the first cell, merge this cell with the cells below it based on the 
                // count of the merged cells. 
                if (isFirstCell)
                {
                    int rowIndex = e.CurrentRowIndex;
                    int colIndex = e.CurrentColumnIndex;
                    Infragistics.Excel.WorksheetMergedCellsRegion mergedRegion = e.CurrentWorksheet.MergedCellsRegions.Add(
                        rowIndex,
                        colIndex,
                        rowIndex + (mergedCells.Length - 1),
                        colIndex);
                    mergedRegion.CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
                    mergedRegion.CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
                }

            }
        }


    }
}
